/*!
 * DbCore
 * version: v1.0.1 (2015-11-12)
 * Andy.Zhou
 */
String.prototype.format = function () {
    if (arguments.length === 0) return this;
    for (var s = this, i = 0; i < arguments.length; i++)
        s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
    return s;
};

var filePath = {
    Rest: "/api/",
    Json: "../Context/Json/"
};
/**
 * common 主要是封装了一些常用的代码库块、字符串验证、字符格式化、func等，不涉及到组件、页面等渲染
 * @module Core
 */
var bdCore = {
    //封装方法
    func: {
        //Ajax方法
        ajax: {
            //异步
            //参考： http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html
            asynchronous:
                function (types, urls, data, outtime) {
                    outtime = outtime ? outtime : 10000;
                    var defers = $.Deferred();
                    var result;
                    if (data == null) {
                        $.ajax({
                            type: types,
                            url: filePath.Rest + urls,
                            contenttype: "json",
                            timeout: outtime,
                            success: function (datare) {
                                result = datare;
                                defers.resolve(datare); //完成 触发.done()
                            },
                            error: function (xmlHttpRequest, error) {
                                result = error;
                                defers.reject(); //失败触发 .fail()
                            }
                        });
                    } else {
                        $.ajax({
                            type: types,
                            url: filePath.Rest + urls,
                            data: data,
                            contenttype: "json",
                            timeout: outtime,
                            success: function (datare) {
                                result = datare;
                                defers.resolve(datare);
                            },
                            error: function (xmlHttpRequest, error) {
                                result = error;
                                defers.reject(); //失败触发 .fail()
                            }
                        });
                    }
                    var a = result == null ? null : result;
                    return defers.promise();

                },
            //同步
            synchronous:
                function (types, urls, data, outtime) {
                    //设置默认时间
                    outtime = outtime ? outtime : 1000;
                    var defer;
                    if (data == null) {
                        $.ajax({
                            type: types,
                            url: filePath.Rest + urls,
                            datatype: "json",
                            contenttype: "json",
                            timeout: outtime,
                            async: false,
                            success: function (datare) {
                                defer = datare;
                            }
                        });
                    } else {
                        $.ajax({
                            type: types,
                            url: filePath.Rest + urls,
                            data: data,
                            datatype: "json",
                            contenttype: "json",
                            timeout: outtime,
                            async: false,
                            success: function (datare) {
                                defer = datare;
                            }
                        });
                    }
                    return defer;
                }
        },
        //html方法
        html: {
            //a标签连接
            A: function (url, text, target, title) {

                target = target ? target : common.properties.html.a.target._self;
                title = title ? title : "";

                var result = " <a target='" + target + "' href='" + url + "' title='" + title + "'>" + text + "</a> ";
                return result;
            }
        }
    },
    //动作
    action: {
        //字符串处理
        string: {
            //拼接字符串(return: a,b,c,d格式)
            addPlus: function () {
                var n = arguments.length;
                var result = "";
                for (var i = 0; i < n; i++) {
                    if (!(arguments[i] === "" || arguments[i] === "null" || arguments[i] === "undefined" || arguments[i] == null)) {
                        if (result === "") {
                            result = arguments[i];
                        } else {
                            result += ", " + arguments[i];
                        }
                    }
                }
                if (result === "") {
                    result = "--";
                }
                return result;
            },
            // 数字匹配单位
            numberAddUnit: function (value, unit) {
                if (common.action.check.string.isNull(value)) {
                    return "N/A";
                }

                value = common.format.numberTenThousand(value);

                //如果没有单位
                if (common.action.check.string.isNull(unit)) {
                    return value;
                }
                var result = value + " " + unit;
                return result;
            },
            isNullOrEey: function (value) {
                if (common.action.check.string.isNull(value)) {
                    return "N/A";
                }
                return value;
            }
        },
        //验证
        check: {
            //数字类
            number: {
                //是否正整数
                integer: function () {
                    var val = arguments;
                    var re = /^[1-9]+[0-9]*]*$/;
                    if (!re.test(val)) {
                        return false;
                    }
                    return true;
                },
                //是否非正整数
                nonInteger: function () {
                    var val = arguments;
                    var re = /^[0-9]+.?[0-9]*$/;
                    if (!re.test(val)) {
                        return false;
                    }
                    return true;
                }
            },
            //字符串类型
            string: {
                //是否不为空
                isNotNull: function (val) {
                    return val !== null && val !== undefined && val !== "";
                },
                //是否为空
                isNull: function (val) {
                    return val === null || val === "" || val === undefined;
                }
            }
        }
    },
    //字符串格式化
    format: {
        //大数值自动匹配单位
        numberTenThousand: function (val) {
            if (val < 9999.99) {
                return common.format.milli(val.toFixed(2));
            }
            if (val > 9999.99 && val < 99999999.99) {
                return common.format.milli((val / 10000).toFixed(2)) + "万";
            }
            if (val > 99999999.99 && val < 999999999999.99) {
                return common.format.milli((val / 100000000).toFixed(2)) + "亿";
            }
            return common.format.milli((val / 1000000000000).toFixed(2)) + "万亿";
        },
        //字符串长度补位和截取
        txtLength: function (str, length) {
            if (str === "" || str == null) {
                return "N/A";
            }
            if (length == null) {
                length = 130;
            }

            if (str.length > length) {
                return str.substring(0, length) + "...";
            }
            return str;
        },
        //添加千位符
        milli: function (num) {
            num = num + "";
            var re = /(-?\d+)(\d{3})/;
            while (re.test(num)) {
                num = num.replace(re, "$1,$2");
            }
            return num;
        }
    },
    //属性
    properties: {
        //js属性
        js: {
            //Ajax请求方式
            ajaxType: {
                Get: "GET",
                POST: "POST",
                Detele: "DETELE",
                Update: "PUT"
            }
        },
        //html标签属性
        html: {
            //a标签属性
            a: {
                //打开连接方式
                target: {
                    _blank: "_blank", //在新窗口中打开被链接文档。
                    _self: "_self", //默认。在相同的框架中打开被链接文档。
                    _parent: "_parent", //在父框架集中打开被链接文档。
                    _top: "_top", //在整个窗口中打开被链接文档。
                    framename: "framename" //在指定的框架中打开被链接文档。
                }
            }
        }
    }
}